//
//  DLSportTrack.m
//  DLSport
//
//  Created by DongL on 2017/4/23.
//  Copyright © 2017年 DongL. All rights reserved.
//

#import "DLSportTrack.h"
#import "DLSportPolyline.h"


@implementation DLSportTrack
{
    //记录的每一段折线的起点
    CLLocation *_source;
}

- (instancetype)initWithSportType:(DLSportType)sportType withSportState:(DLSportState)state{
    self = [super init];
    if (self) {
        
        //记录运动类型
        _sportType = sportType;
        //设置运动类型图片
        //判断运动类型
        NSString *sportTypeImgName;
        switch (_sportType) {
            case DLSportTypeRunning:
                sportTypeImgName = @"map_annotation_run";
                break;
            case DLSportTypeRiding:
                sportTypeImgName = @"map_annotation_bike";
                break;
            case DLSportTypeWalking:
                sportTypeImgName = @"map_annotation_walk";
                break;
            default:
                break;
        }
        _sportTypeImgName = sportTypeImgName;
        _sportState = state;
    }
    return self;
}


- (DLSportPolyline *)appendPolylineWithDest:(CLLocation *)dest {
    
    
    //判断是否移动 速度=0   当手机在室内时,speed为负值,表明室内GPS被干扰,速度无效
    if (dest.speed <= 0) {
        
        return nil;
    }
    
    //排除已经"过期"的定位数据  计算当前时间和定位时间戳的差值  2秒以内的数据可用
    if ([[NSDate date] timeIntervalSinceDate:dest.timestamp] >= 2) {
        
        return nil;
    }
    
    //判断运动状态
    if (self.sportState != DLSportStateContinue) {
        //暂停/结束运动时,停止生成折线
        //清空记录的折线起点
        _source = nil;
        return nil;
    }
    
    //判断首次调用
    if (_source == nil) {
        
        //记录起点
        _source = dest;
        //生成起点大头针模型
        _startAnno = [[MAPointAnnotation alloc] init];
        _startAnno.coordinate = dest.coordinate;
        
        return nil;
    }
    
    //创建折线
    DLSportPolyline *polyline = [DLSportPolyline polylineWithSourceLocation:_source andDestLocation:dest];
    
    
    //记录下一段的起点
    _source = dest;
    
    return polyline;

    
}


@end
